// merchantApply.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

const app = getApp()
const area = require("../../utils/area.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperbox: app.globalData.swiperbox,
    swiper: app.globalData.swiper,
    form: {
      shop:'',
      username: '',
      phone: '',
      number: '',
      industry: '',
      province: '北京市',
      city: '北京市',
      area: '东城区',
      areaShow: false,
      address: ''
    },
    areaList: area.areaList
  },
  toggle(type) {
    this.setData({
      [type]: !this.data[type]
    });
  },
  onChange(detail, event) {// detail 为当前输入的值
    this.setData({
      [detail]: event.detail
    })
  },
  nameChange: function (event) {
    this.onChange('form.username', event)
  },
  telChange: function (event) {
    this.onChange('form.phone', event)
  },
  shopChange: function (event) {
    this.onChange('form.shop', event)
  },
  numChange: function (event) {
    this.onChange('form.number', event)
  },
  addressChange: function (event) {
    this.onChange('form.address', event)
  },
  industryTap: function (event) {
    this.onChange('form.industry', event)
  },
  // 地区选择
  areaTap: function () {
    this.toggle('form.areaShow');
  },
  areaClose: function () {
    var show = 'form.areaShow';
    this.setData({ [show]: false });
  },
  areaConfirm: function (event) {
    var province = 'form.province';
    var city = 'form.city';
    var area = 'form.area';
    var show = 'form.areaShow';
    var values = event.detail.values;
    var areaTxt = ''
    if (values[2] == undefined) {
      areaTxt = ''
    } else {
      areaTxt = values[2].name
    }
    this.setData({
      [province]: values[0].name,
      [city]: values[1].name,
      [area]: areaTxt,
      [show]: false
    });

  },
  // 获取详细地址
  getAddress: function () {
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        wx.chooseLocation({
          latitude,
          longitude,
          success: function (res) {
            var address = 'form.address'
            that.setData({
              [address]: res.name
            })
          },
        })
      },fail(res){
        Toast.fail("请检查您的微信或小程序是否开启定位服务")
      }
    })
  },
  // 提交
  submit: function (e) {
    var that = this;
    var form = that.data.form;
    var data = {
      shop: form.shop,
      channel:form.industry,
      size: form.number,
      name:form.username,
      tel:form.phone,
      province: form.province,
      city: form.city,
      area: form.area,
      address:form.address,
      type: 'shop'
      };//要传的数组对象
    wx.showLoading({
      title: '加载中...',
    })
    if (form.shop !== '' && form.industry !== '' && form.size !== '' && form.username !== '' && form.phone !== '' &&  form.province !== '' && form.city !== '' && form.address !== ''){
      //调用 app.js里的 post()方法
      app.post('identity/agentApply', data).then((res) => {
        // console.log(res);//正确返回结果
        wx.hideLoading();
        if(res.code == 200){
          wx.showToast({
            title: res.message,
            icon: 'success',
            duration: 2000
          })
          wx.navigateBack({
            delta: 1
          })
        }else{
          wx.showToast({
            title: "提交失败",
            icon: 'loading',
            duration: 2000
          })
        }
      }).catch((errMsg) => {
        console.log(errMsg);//错误提示信息
        wx.hideLoading();
        wx.showToast({
          title: errMsg,
          icon: 'loading',
          duration: 2000
        })
      });
    } else {
      wx.showToast({
        title: '请完善您的资料',
        icon: 'none',
        duration: 4000
      })
    }
    
  },
  onLoad: function (options) {
    this.setData({
      swiper: wx.getStorageSync('swiper') || app.globalData.swiper,
    })
  },

})